package com.atl.utils;

public class IdGenerator {
    private static long lastId;
    private static long sequence = 0L;
    public static synchronized long nextId() {
        long currentId = System.currentTimeMillis();
        if (currentId == lastId) {   // 如果当前时间等于上一次ID生成时间
            sequence++;               // sequence自增1
            if (sequence > 999) {     // 如果sequence超过1000,则重置为0
                currentId += 1;       // 当前时间增加1ms
                sequence = 0;
            }
        } else {
            sequence = 0;            // 更新时间戳,sequence重置为0
        }
        lastId = currentId;          // 更新上次生成ID时间
        return currentId * 100 + sequence;  // ID由时间戳与sequence构成
    }
}
